package com.gitee.wsl.ext.array.bytes

import com.gitee.wsl.ext.array.emptyByteArray
import com.gitee.wsl.io.buffer.ByteBuffer
import com.gitee.wsl.io.buffer.buffered
import com.gitee.wsl.io.buffer.readContent
import com.gitee.wsl.lang.ByteOrder

fun DoubleArray.toByteArray(byteOrder: ByteOrder=ByteOrder.BigEndian): ByteArray {
    if(isEmpty()) return emptyByteArray

    val buffer: ByteBuffer = ByteBuffer.allocate(size * 8,byteOrder)
    for (i in indices) {
        buffer.double = this[i]
    }
    return buffer.contentBytes
}

fun ByteArray.toDoubleArray(byteOrder: ByteOrder=ByteOrder.BigEndian): DoubleArray{
    val list = mutableListOf<Double>()
    this.buffered(byteOrder).readContent { list.add(double) }
    return list.toDoubleArray()
}